package com.chijun.mform.full;

/*









                                   .                            .
                           .:-====+++====-:.            .:-====+++====-:.
                        :-=+++++++++++++++++==:.    .:==+++++++++++++++++=-:
                     .-=+++====+++++++++====++++=::=++++====+++++++++====+++=-.
                    -+++==+++++++++++++++++++==++++++==+++++++++++++++++++==+++-
                  .=+==+++++++++++++++++++++++++====+++++++++++++++++++++++++==+=.
                 .=+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+=.
                 =+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+=
                :+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+:
                -+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+-
                =+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+=
                -+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+-
                :+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+:
                 ==++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
                 :+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+:
                  :+=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=+:
                   :++=++++++++++++++++++++++++++++++++++++++++++++++++++++++=++:
                    :=+=++++++++++++++++++++++++++++++++++++++++++++++++++++=+=:
                     .=++=++++++++++++++++++++++++++++++++++++++++++++++++=++=.
                       :=+==++++++++++++++++++++++++++++++++++++++++++++==+=:
                        .-++==++++++++++++++++++++++++++++++++++++++++==++-.
                          .=++==++++++++++++++++++++++++++++++++++++==++=.
                            .=++==++++++++++++++++++++++++++++++++==++=.
                              :=++==++++++++++++++++++++++++++++==++=:
                                .=+++=++++++++++++++++++++++++==++=.
                                  .-+++=++++++++++++++++++++=+++-.
                                    .-=++=++++++++++++++++=+++-.
                                      .-=++==++++++++++==++=-.
                                         :=++==++++++==++=:
                                           :=++==++==++=:
                                             .-++++++=.
                                               .-==-.


 */


import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.IWaitResult;
import com.chijun.mform.create.mode.Form;
import com.chijun.mform.full.mode.ModeLoadServerFormConfigData;
import com.chijun.mform.full.mode.ModeServerSubmitData;
import com.chijun.mform.full.view.ViewLoadForm;
import com.chijun.mform.test.CreateFormServiceImpl;
import com.example.ccj.b.a.BaseActivity;
import com.example.ccj.b.c.dialog.ErrorDialog;
import com.example.ccj.u.JsonUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.server.MyObserver;
import com.example.ccj.u.server.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 填写表单
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/31 1:35
 */
public class ActivityFormFullIn extends BaseActivity implements IWaitResult,IHandleWriterData {
    ViewLoadForm loadFormView;

    @Override
    public void initData(@Nullable Bundle bundle) {
        if (bundle != null) {
            String form_data = bundle.getString("form_data");
            String form_id = bundle.getString("form_id");

            if (form_data != null) {
                ModeLoadServerFormConfigData writeFormData = ModeLoadServerFormConfigData.analysis(form_data);
                if (writeFormData != null) {
                    showTopBar (String.valueOf(writeFormData.form.name));
                    loadFormView.setFormData(writeFormData, this,this);
                } else {
                    finish();
                }
            } else if (form_id != null) {
                loadFormView.setFormData(form_id,this, this);
            } else {
                LogUtils.e("form_data和form_id必须传进来一个值！");
            }
        } else {
            ToastUtils.showShort("未获取到数据，退出!");
            finish();
        }
    }

    @Override
    public void initView(@Nullable Bundle bundle, @Nullable View view) {

        loadFormView = findViewById(R.id.activity_form_full_in_load);
    }

    @Override
    public int initContentView() {
        showTopBar("填写表单");
        return R.layout.activity_form_full_in;
    }

    @Override
    public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {

    }

    @Override
    public void onResult(String val) {
        ToastUtils.showLong("保存完毕");
        finish();
    }

    @Override
    public void onError(String error) {
        ErrorDialog errorDialog = new ErrorDialog(this,error);
        errorDialog.show();
    }



    @Override
    public void getAllForm(IGetFieldAllFormListener getFieldAllFormListener) {
        CreateFormServiceImpl.getInstance().getAllForm("")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        List<Form> listBean = JsonUtils.toListBean(result.getData(), Form.class);
                        getFieldAllFormListener.onResult(listBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getFieldAllFormListener.onError(e.getMessage());
                    }
                });
    }

    @Override
    public void uploadServer(ModeServerSubmitData saveFormData, IWaitResult iWaitResult) {
        Map<String,String> submitMap = new HashMap<>();
        submitMap.put("form_data", JSON.toJSONString(saveFormData.launchedForm));
        submitMap.put("form_file_data",JSON.toJSONString(saveFormData.formFullFileData));
        submitMap.put("form_approve_person",JSON.toJSONString(saveFormData.formFullApprovePerson));
        submitMap.put("form_or_field_data",JSON.toJSONString(saveFormData.formOrdinaryFieldData));

        CreateFormServiceImpl.getInstance().saveFormData(JSON.toJSONString(submitMap))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        iWaitResult.onResult(result.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        iWaitResult.onError(e.getMessage());
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getFormFullConfig(String formId, IResultFormConfig iResultFormConfig) {
        CreateFormServiceImpl.getInstance().getFillInFormData(formId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        iResultFormConfig.result(result.getData());
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        iResultFormConfig.onError(e.getMessage());
                    }
                });
    }

}